package ls.vuecp.gui;

import javax.swing.Action;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import tufts.vue.VueResources;

import ls.vuecp.action.Actions;
import ls.vuecp.action.LayoutAction;
import ls.vuecp.action.VueAction;

public class VueMenuBar extends JMenuBar
{
	private static VueMenuBar mthis=null;
	public static VueMenuBar getVueMenuBar(){ if( mthis == null )	mthis = new VueMenuBar();	return mthis; }
	public VueMenuBar()
	{
		mthis = this;
	}
	public void buildMenu()
	{
		buildFileMenu();
		buildEditMenu();
		buildViewMenu();
		buildFormatMenu();
		buildHelpMenu();
		
		add(fileMenu);
		add(editMenu);
		add(viewMenu);
		add(formatMenu);
		add(helpMenu);
	}
	
	// top-level menus;
	private JMenu fileMenu;
	private JMenu editMenu;
	private JMenu viewMenu;
	private JMenu formatMenu;
	private JMenu contentMenu;
	private JMenu presentationMenu;
	private JMenu analysisMenu;
	private JMenu helpMenu;
	
	// File Menu sub;
	public static JMenu importMenu = null;
    public static JMenu publishMenu = null;
    public static JMenu pdfExportMenu = null;
    
    // view menu sub
    private final JCheckBoxMenuItem viewFullScreen = makeLinkedCheckBox(
    		Actions.ToggleFullScreen);
    private final JCheckBoxMenuItem viewSuperScreen = new JCheckBoxMenuItem(
    		Actions.SuperScreen);
    private final JCheckBoxMenuItem viewKiosk =new JCheckBoxMenuItem(
    		Actions.KioskScreen);
    
	// format menu sub;
    public static JMenu transformMenu = null;
    public static JMenu arrangeMenu = null;
    public static JMenu alignMenu = null;
    public static JMenu layoutMenu = null;
    public static JMenu linkMenu = null;
    public static JMenu playbackMenu = null;
    public boolean isMenuEnableFontFlg = false;
    
    private void buildFileMenu()
	{
		fileMenu = makeMenu("File");
		
		// pdf export;
		final JMenuItem fullPageSlideNotesItem = new JMenuItem(Actions.fullPageSlideNotesAction);
		final JMenuItem slides8PerPageItem = new JMenuItem(Actions.slides8PerPageAction);
		final JMenuItem speakerNotes1Item = new JMenuItem(Actions.speakerNotes1Action);
		final JMenuItem speakerNotes4Item = new JMenuItem(Actions.speakNotes4Action);
		final JMenuItem audienceNotesItem = new JMenuItem(Actions.audienceNotesAction);
		final JMenuItem speakerNotesOutlineItem = new JMenuItem(Actions.speakNotesOutlineAction);
		final JMenuItem nodeNotes4Item = new JMenuItem(Actions.nodeNotes4Action);
		final JMenuItem nodeNotesOutlineItem = new JMenuItem(Actions.nodeNotesOutlineAction);
		
		pdfExportMenu = new JMenu("Export Handouts");
		pdfExportMenu.setEnabled(false);
		pdfExportMenu.add(fullPageSlideNotesItem);
        pdfExportMenu.add(slides8PerPageItem);
        pdfExportMenu.add(speakerNotes1Item);
        pdfExportMenu.add(speakerNotes4Item);
        pdfExportMenu.add(audienceNotesItem);        
        pdfExportMenu.add(speakerNotesOutlineItem);
        pdfExportMenu.addSeparator();
        pdfExportMenu.add(nodeNotes4Item);
        pdfExportMenu.add(nodeNotesOutlineItem); 
        
        // exit;
//		JMenuItem exitItem = new JMenuItem("Exit");
		JMenuItem exitItem = new JMenuItem(VueResources.getString("menu.windows.exit"));
        exitItem.setMnemonic('x');
        exitItem.addActionListener(Actions.exitAction);
		
		//fileMenu.add(Actions.defaultAction);
        fileMenu.add(Actions.newMapAction);
        fileMenu.add(Actions.openAction);
        fileMenu.add(Actions.openFromUrlAction);
        fileMenu.add(Actions.closeMapAction);
        
		fileMenu.addSeparator();
		fileMenu.add(Actions.saveAction);
		fileMenu.add(Actions.saveAsAction);
		fileMenu.add(Actions.revertAction);	Actions.revertAction.setEnabled(false);
		
		fileMenu.addSeparator();
		fileMenu.add(Actions.quickImportAction);
		
		fileMenu.addSeparator();
		fileMenu.add(Actions.printAction);
		fileMenu.add(pdfExportMenu);
		
		fileMenu.addSeparator();
		fileMenu.add(exitItem);
	}
    private void buildEditMenu()
    {
    	editMenu = makeMenu("Edit");
    	
    	editMenu.add(Actions.Undo);
        editMenu.add(Actions.Redo);
        editMenu.addSeparator();
        editMenu.add(Actions.Cut);
        editMenu.add(Actions.Copy);
        editMenu.add(Actions.Paste);
        editMenu.add(Actions.Duplicate);
        editMenu.add(Actions.Rename);
        editMenu.add(Actions.Delete);   
        
        editMenu.addSeparator();
        editMenu.add(Actions.SelectAll);
        editMenu.add(Actions.SelectAllNodes);
        editMenu.add(Actions.SelectAllLinks);
        editMenu.add(Actions.ExpandSelection);
        editMenu.add(Actions.ShrinkSelection);
        editMenu.add(Actions.Reselect);
        editMenu.add(Actions.DeselectAll);
        
        editMenu.addSeparator();
        editMenu.add(Actions.Preferences);
    }
    private void buildViewMenu()
    {
    	viewMenu = makeMenu("View");
    	
    	viewMenu.add(Actions.ZoomIn);
        viewMenu.add(Actions.ZoomOut);
        viewMenu.addSeparator();            
        viewMenu.add(Actions.ZoomFit);
        viewMenu.add(Actions.ZoomToSelection);
        viewMenu.add(Actions.ZoomActual);
        
        viewMenu.addSeparator();
        viewMenu.add(viewFullScreen);
        viewMenu.add(viewSuperScreen);
        
        viewMenu.addSeparator();
        //viewMenu.add(toggleSlideIconItem);
        viewMenu.addSeparator();
        //viewMenu.add(togglePruningItem);
        viewMenu.add(Actions.ClearAllPruning);
    }
    private void buildFormatMenu()
    {
    	formatMenu = makeMenu("Foramt");
    	
    	transformMenu = makeMenu("Font");//VueResources.getString("menu.font"));
        transformMenu.setEnabled(false);
        transformMenu.add(Actions.FontBigger);
        transformMenu.add(Actions.FontSmaller);
        transformMenu.add(Actions.FontBold);
        transformMenu.add(Actions.FontItalic);      
        transformMenu.add(Actions.FontUnderline);
            
        arrangeMenu = makeMenu("Arrange");//VueResources.getString("menu.arrange"));
        arrangeMenu.setEnabled(false);       
        alignMenu = makeMenu("Align");//VueResources.getString("menu.align"));
        alignMenu.setEnabled(false);      
        layoutMenu = makeMenu("Layout");//VueResources.getString("menu.layout"));
        layoutMenu.setEnabled(false);               
        linkMenu = makeMenu("Link");//VueResources.getString("menu.link"));
        linkMenu.setEnabled(false);
        buildMenu(alignMenu, Actions.ALIGN_MENU_ACTIONS);
        buildMenu(arrangeMenu, Actions.ARRANGE_MENU_ACTIONS);
        buildMenu(layoutMenu,LayoutAction.LAYOUT_ACTIONS);
        buildMenu(linkMenu, Actions.LINK_MENU_ACTIONS);
        
        final JMenu notesMenu = new JMenu("Handout sand notes");//VueResources.getString("menu.pathways.handoutsandnotes"));
        playbackMenu = new JMenu("Playback");//VueResources.getString("menu.playback.play.label"));
        playbackMenu.setEnabled(false); 
        
        formatMenu.add(buildMenu("menu.node",Actions.NODE_FORMAT_MENU_ACTIONS));
        formatMenu.add(transformMenu);
        formatMenu.add(buildMenu("menu.image", Actions.IMAGE_MENU_ACTIONS));
        formatMenu.add(linkMenu);
        formatMenu.add(alignMenu);
        formatMenu.add(arrangeMenu);
        formatMenu.add(layoutMenu);
        
        formatMenu.addSeparator();
        formatMenu.add(Actions.Group);
        formatMenu.add(Actions.Ungroup);
        
        formatMenu.addSeparator();
    }
    
    private void buildContentMenu()
    {
    	contentMenu = makeMenu("Content");//VueResources.getString("menu.content"));
    }
    private void buildPresentationMenu()
    {
    	presentationMenu = makeMenu("Presentation");//VueResources.getString("menu.pathway.label"));
    }
    private void buildAnalysisMenu()
    {
    	analysisMenu = makeMenu("Analysis");//VueResources.getString("menu.analysis"));
    }
    private void buildHelpMenu()
    {
    	helpMenu = add(makeMenu("Help"));//VueResources.getString("menu.help")));
        
    	helpMenu.add(Actions.About);
    }
    
    
    
    
    
    JMenu makeMenu(String name)
    {
    	return new VueMenu(name);
    }
    public static JMenu buildMenu(String localizationKey, Action[] actions) {
        return buildMenu(localizationKey, actions, true);
    }
        
    public static JMenu buildMenu(String localizationKey, Action[] actions, boolean enabled) {
        final JMenu menu = new JMenu(localizationKey);//VueResources.getString(localizationKey, localizationKey));
        buildMenu(menu, actions);
        if (!enabled)
            menu.setEnabled(false);
        return menu;
    }
    
    public static JMenu buildMenu(JMenu menu, Action[] actions) {
        for (int i = 0; i < actions.length; i++) {
            Action a = actions[i];
            if (a == null)
                menu.addSeparator();
            else
                menu.add(a);
        }
        return menu;
    }
    private static JCheckBoxMenuItem makeCheckBox(Action a) {
        return makeCheckBox((VueAction) a);
    }
    
    private static JCheckBoxMenuItem makeCheckBox(VueAction a) {
        return makeCheckBox(a, a.isToggler() ? a.getToggleState() : false);
    }
    private static JCheckBoxMenuItem makeCheckBox(VueAction a, boolean initialState) {
        final JCheckBoxMenuItem item = new JCheckBoxMenuItem(a);
        //final JCheckBoxMenuItem item = new JCheckBoxMenuItem(a.getPermanentActionName());
        if (a.getKeyStroke() != null)
            item.setAccelerator(a.getKeyStroke());
        item.setSelected(initialState);
        return item;
    }
    private static JCheckBoxMenuItem makeLinkedCheckBox(VueAction a) {
        final JCheckBoxMenuItem item = makeCheckBox(a);
        a.trackToggler(item);
        return item;
    }
}
